package cn.flying.cloud.system.tenant.service.impl;

import javax.annotation.Resource;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.common.enums.SeqTypeEnum;
import cn.flying.cloud.system.log.service.SysSeqInfoService;
import cn.flying.cloud.system.tenant.entity.SysTenantInfo;
import cn.flying.cloud.system.tenant.mapper.SysTenantInfoMapper;
import cn.flying.cloud.system.tenant.service.SysTenantInfoService;

/**
 * 租户信息表 服务实现类
 *
 * @author admin
 * @since 2024-06-13 13:15:43
 */
@Service
public class SysTenantInfoServiceImpl extends ServiceImpl<SysTenantInfoMapper, SysTenantInfo> implements SysTenantInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysSeqInfoService sysSeqInfoService;

    /**
     * 分页查询租户信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysTenantInfo> getPageList(PageRequest<SysTenantInfo> request) {
        SysTenantInfo param = request.getParam();
        LambdaQueryWrapper<SysTenantInfo> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        Page<SysTenantInfo> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysTenantInfo> getLambdaQueryWrapper(SysTenantInfo param) {
        LambdaQueryWrapper<SysTenantInfo> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getTenantId())) {
                wrapper.eq(SysTenantInfo::getTenantId, param.getTenantId());
            }
            if (Ft.String.isNotBlank(param.getTenantName())) {
                wrapper.eq(SysTenantInfo::getTenantName, param.getTenantName());
            }
            if (Ft.String.isNotBlank(param.getStatus())) {
                wrapper.eq(SysTenantInfo::getStatus, param.getStatus());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询租户信息表数据
     *
     * @param sysTenantInfo
     * @return
     */
    @Override
    public List<SysTenantInfo> getList(SysTenantInfo sysTenantInfo) {
        LambdaQueryWrapper<SysTenantInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        List<SysTenantInfo> list = this.list(wrapper);
        return list;
    }

    /**
     * 查询租户信息表详情数据
     *
     * @param sysTenantInfo
     * @return
     */
    @Override
    public SysTenantInfo getOneObj(SysTenantInfo sysTenantInfo) {
        LambdaQueryWrapper<SysTenantInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.getOne(wrapper);
    }


    /**
     * 新增租户信息表数据
     *
     * @param sysTenantInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysTenantInfo sysTenantInfo) {
        sysTenantInfo.setTenantId(sysSeqInfoService.getNextSeq(SeqTypeEnum.TENANT.name()));
        return this.save(sysTenantInfo);
    }

    /**
     * 更新租户信息表数据
     *
     * @param sysTenantInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysTenantInfo sysTenantInfo) {
        return this.saveOrUpdate(sysTenantInfo);
    }

    /**
     * 删除租户信息表数据
     *
     * @param sysTenantInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysTenantInfo sysTenantInfo) {
        LambdaQueryWrapper<SysTenantInfo> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
